package cn.xz.kjg.activity;


import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Objects;

import cn.xz.kjg.R;
import cn.xz.kjg.bean.CommonLib;
import cn.xz.kjg.net.SendUdp;

/**
 * 虚拟键盘
 */
public class SJActivity extends AppCompatActivity {

    private TextView q, w, e, r, t, y, u, i, o, p, a, s, d, f, g, h, j, k, l, z, x, c, v_t, b, n, m, fu,caps,tab,shift,enter;

    private LinearLayout llMove;

    private TextView left, right;

    private boolean isBig = false;

    private String mIp;

    private final int mPort = 6001;

    private long downTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s_j);

        initData();
        initView();
        initEvent();
    }

    private void initData(){
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()   // or .detectAll() for all detectable problems
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
                .penaltyLog()
                .penaltyDeath()
                .build());

        mIp = getIntent().getStringExtra(CommonLib.JUMP_TYPE);

    }

    private void initView(){
        q = (TextView) findViewById(R.id.xnsj_q);
        w = (TextView) findViewById(R.id.xnsj_w);
        e = (TextView) findViewById(R.id.xnsj_e);
        r = (TextView) findViewById(R.id.xnsj_r);
        t = (TextView) findViewById(R.id.xnsj_t);
        y = (TextView) findViewById(R.id.xnsj_y);
        u = (TextView) findViewById(R.id.xnsj_u);
        i = (TextView) findViewById(R.id.xnsj_i);
        o = (TextView) findViewById(R.id.xnsj_o);
        p = (TextView) findViewById(R.id.xnsj_p);
        a = (TextView) findViewById(R.id.xnsj_a);
        s = (TextView) findViewById(R.id.xnsj_s);
        d = (TextView) findViewById(R.id.xnsj_d);
        f = (TextView) findViewById(R.id.xnsj_f);
        g = (TextView) findViewById(R.id.xnsj_g);
        h = (TextView) findViewById(R.id.xnsj_h);
        j = (TextView) findViewById(R.id.xnsj_j);
        k = (TextView) findViewById(R.id.xnsj_k);
        l = (TextView) findViewById(R.id.xnsj_l);
        z = (TextView) findViewById(R.id.xnsj_z);
        x = (TextView) findViewById(R.id.xnsj_x);
        c = (TextView) findViewById(R.id.xnsj_c);
        v_t = (TextView) findViewById(R.id.xnsj_v);
        b = (TextView) findViewById(R.id.xnsj_b);
        n = (TextView) findViewById(R.id.xnsj_n);
        m = (TextView) findViewById(R.id.xnsj_m);
        fu = (TextView) findViewById(R.id.xnsj_fn);
        tab = (TextView) findViewById(R.id.xnsj_tab);
        shift = (TextView) findViewById(R.id.xnsj_shift);
        enter = (TextView) findViewById(R.id.xnsj_enter);

        llMove = (LinearLayout) findViewById(R.id.ll_move);
        caps = (TextView) findViewById(R.id.xnsj_caps);
        left = (TextView) findViewById(R.id.xnsj_left);
        right = (TextView) findViewById(R.id.xnsj_right);
    }

    private void initEvent(){
        llMove.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        int x = (int) event.getX();
                        int y = (int) event.getY();
                        Log.e("x->y:", x + "->" + y);
                        final String message = downMsg(x, y, 0);
                        SendUdp.send(message, mIp, mPort);
                        downTime = System.currentTimeMillis();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int moveX = (int) event.getX();
                        int moveY = (int) event.getY();
                        Log.e("move x->y:", moveX + "->" + moveY);
                        final String moveM = downMsg(moveX, moveY, 1);
                        SendUdp.send(moveM, mIp, mPort);
                        break;
                    case MotionEvent.ACTION_UP:
                        int upX = (int) event.getX();
                        int upY = (int) event.getY();
                        Log.e("up x->y:", upX + "->" + upY);

                        if ((System.currentTimeMillis() - downTime) < 200) {
                            SendUdp.send("{LeftDown}", mIp, mPort);
                        }
                        SendUdp.send("{LeftUp}", mIp, mPort);
                        break;
                }
                return false;
            }
        });
        left.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    SendUdp.send("{LeftDown}", mIp, mPort);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    SendUdp.send("{LeftUp}", mIp, mPort);
                }
                return false;
            }
        });
        right.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    SendUdp.send("{RightDown}", mIp, mPort);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    SendUdp.send("{RightUp}", mIp, mPort);
                }
                return false;
            }
        });
    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.xnsj_1:
                sendUDP("{1}");
                break;
            case R.id.xnsj_2:
                sendUDP("{2}");
                break;
            case R.id.xnsj_3:
                sendUDP("{3}");
                break;
            case R.id.xnsj_4:
                sendUDP("{4}");
                break;
            case R.id.xnsj_5:
                sendUDP("{5}");
                break;
            case R.id.xnsj_6:
                sendUDP("{6}");
                break;
            case R.id.xnsj_7:
                sendUDP("{7}");
                break;
            case R.id.xnsj_8:
                sendUDP("{8}");
                break;
            case R.id.xnsj_9:
                sendUDP("{9}");
                break;
            case R.id.xnsj_0:
                sendUDP("{0}");
                break;
            case R.id.xnsj_q:
                sendUDP(gText(q.getText().toString()));
                break;
            case R.id.xnsj_w:
                sendUDP(gText(w.getText().toString()));
                break;
            case R.id.xnsj_e:
                sendUDP(gText(e.getText().toString()));
                break;
            case R.id.xnsj_r:
                sendUDP(gText(r.getText().toString()));
                break;
            case R.id.xnsj_t:
                sendUDP(gText(t.getText().toString()));
                break;
            case R.id.xnsj_y:
                sendUDP(gText(y.getText().toString()));
                break;
            case R.id.xnsj_u:
                sendUDP(gText(u.getText().toString()));
                break;
            case R.id.xnsj_i:
                sendUDP(gText(i.getText().toString()));
                break;
            case R.id.xnsj_o:
                sendUDP(gText(o.getText().toString()));
                break;
            case R.id.xnsj_p:
                sendUDP(gText(p.getText().toString()));
                break;
            case R.id.xnsj_a:
                sendUDP(gText(a.getText().toString()));
                break;
            case R.id.xnsj_s:
                sendUDP(gText(s.getText().toString()));
                break;
            case R.id.xnsj_d:
                sendUDP(gText(d.getText().toString()));
                break;
            case R.id.xnsj_f:
                sendUDP(gText(f.getText().toString()));
                break;
            case R.id.xnsj_g:
                sendUDP(gText(g.getText().toString()));
                break;
            case R.id.xnsj_h:
                sendUDP(gText(h.getText().toString()));
                break;
            case R.id.xnsj_j:
                sendUDP(gText(j.getText().toString()));
                break;
            case R.id.xnsj_k:
                sendUDP(gText(k.getText().toString()));
                break;
            case R.id.xnsj_l:
                sendUDP(gText(l.getText().toString()));
                break;
            case R.id.xnsj_z:
                sendUDP(gText(z.getText().toString()));
                break;
            case R.id.xnsj_x:
                sendUDP(gText(x.getText().toString()));
                break;
            case R.id.xnsj_c:
                sendUDP(gText(c.getText().toString()));
                break;
            case R.id.xnsj_v:
                sendUDP(gText(v_t.getText().toString()));
                break;
            case R.id.xnsj_b:
                sendUDP(gText(b.getText().toString()));
                break;
            case R.id.xnsj_n:
                sendUDP(gText(n.getText().toString()));
                break;
            case R.id.xnsj_m:
                sendUDP(gText(m.getText().toString()));
                break;
            case R.id.xnsj_pgup:
                sendUDP("{PAGEUP}");
                break;
            case R.id.xnsj_pgdn:
                sendUDP("{PAGEDOWN}");
                break;
            case R.id.xnsj_del:
                sendUDP("{DEL}");
                break;
            case R.id.xnsj_left:
                break;
            case R.id.xnsj_right:
                break;
            case R.id.xnsj_que:
                sendUDP("{ENTER}");
                break;
            case R.id.xnsj_tab:
                sendUDP(gText(tab.getText().toString()));
                break;
            case R.id.xnsj_shift:
                sendUDP(gText(shift.getText().toString()));
                break;
            case R.id.xnsj_enter:
                sendUDP(gText(enter.getText().toString()));
                break;
            case R.id.xnsj_caps:
                if (caps.getText().toString().contains("F4")){
                    sendUDP("{ALTF4}");
                }else {
                    isBig = !isBig;
                    changeTv();
                }
                break;
            case R.id.xnsj_back:
                finish();
                break;
            case R.id.xnsj_fn: //切换符号
                setF();
                break;
            case R.id.xnsj_ctrl:
                sendUDP("{ESC}");
                break;
            default:
                break;
        }
    }

    //发送UDP消息
    private void sendUDP(String msg){
        Log.e("TAG", "sendUDP: 测试模拟键盘数据--->" + msg);
        SendUdp.send(msg, mIp, mPort);
    }

    //拼接消息体
    private String gText(String s) {
        if (Objects.equals(s, "空格")){
            s = "SPAC";
        }
        if (Objects.equals(s, "Tab")){
            s = "TAB";
        }
        if (Objects.equals(s, "Shift")){
            s = "SHIFT";
        }
        if (Objects.equals(s, "Enter")){
            s = "ENTER";
        }
        return "{" + s + "}";
    }

    //移动消息
    public String downMsg(int x, int y, int type) {//type:0为down,1为移动
        String msg = null;
        if (type == 0) {
            msg = "{B|" + 2 * x / 3 + "|" + 2 * y / 3 + "}";
        } else if (type == 1) {
            msg = "{D|" + 2 * x / 3 + "|" + 2 * y / 3 + "}";
        }
        return msg;
    }

    //切换大小写
    public void changeTv() {
        if (isBig) {
            q.setText("Q");
            w.setText("W");
            e.setText("E");
            r.setText("R");
            t.setText("T");
            y.setText("Y");
            u.setText("U");
            i.setText("I");
            o.setText("O");
            p.setText("P");
            a.setText("A");
            s.setText("S");
            d.setText("D");
            f.setText("F");
            g.setText("G");
            h.setText("H");
            j.setText("J");
            k.setText("K");
            l.setText("L");
            z.setText("Z");
            x.setText("X");
            c.setText("C");
            v_t.setText("V");
            b.setText("B");
            n.setText("N");
            m.setText("M");
        } else {
            q.setText("q");
            w.setText("w");
            e.setText("e");
            r.setText("r");
            t.setText("t");
            y.setText("y");
            u.setText("u");
            i.setText("i");
            o.setText("o");
            p.setText("p");
            a.setText("a");
            s.setText("s");
            d.setText("d");
            f.setText("f");
            g.setText("g");
            h.setText("h");
            j.setText("j");
            k.setText("k");
            l.setText("l");
            z.setText("z");
            x.setText("x");
            c.setText("c");
            v_t.setText("v");
            b.setText("b");
            n.setText("n");
            m.setText("m");
        }
    }

    //切换符号
    private void setF(){
        if (fu.getText().toString().contains("符")){
            fu.setText("拼");
            caps.setText("Alt+F4");
            enter.setText("空格");
            tab.setText("~");
            shift.setText("_");
            q.setText("!");
            w.setText("@");
            e.setText("#");
            r.setText("$");
            t.setText("%");
            y.setText("&");
            u.setText("*");
            i.setText("(");
            o.setText(")");
            p.setText("?");
            a.setText("+");
            s.setText("-");
            d.setText("*");
            f.setText("/");
            g.setText("=");
            h.setText("{");
            j.setText("}");
            k.setText("[");
            l.setText("]");
            z.setText(":");
            x.setText(";");
            c.setText("|");
            v_t.setText("\\");
            b.setText(",");
            n.setText(".");
            m.setText("~");
        }else {
            fu.setText("符");
            caps.setText("Caps");
            tab.setText("Tab");
            shift.setText("Shift");
            enter.setText("Enter");
            changeTv();
        }
    }
}
